package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
<<<<<<< HEAD
import com.itheima.health.entity.Result;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
=======
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
>>>>>>> 6693e98994950fffdaffe5b5f8491e5db91a2da2
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
<<<<<<< HEAD

/**
 * @Author:hujunwei
 * @Description:
 * @Date:2019/7/30
 * @Version:1.0
=======
import java.util.Map;

/**
 * @author:
 * @data : Created in 2019/7/29
 * @description:  菜单控制器
 * @version:1.0
>>>>>>> 6693e98994950fffdaffe5b5f8491e5db91a2da2
 */
@RestController
@Slf4j
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;
<<<<<<< HEAD

    /*
     * @Author:hujunwei
     * @功能:查询所有菜单，用于新增角色弹窗页面回显
     * @date: 2019/7/30
     * @param
     * @return com.itheima.health.entity.Result
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        return menuService.findAll();
    }

    /*
     * @Author:hujunwei
     * @功能:新增角色时,通过角色id返回所有与之对应的菜单id，用于页面回显
     * @date: 2019/7/30
     * @param id
     * @return com.itheima.health.entity.Result
     */
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){//这里的id要与前端页面传递过来的参数一摸一样
        try {
            List<Integer> menuIds = menuService.findMenuIdsByRoleId(id);
            return new Result(true,"返回菜单ids成功",menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"返回菜单ids失败");
        }
    }
=======
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Menu> menuList = menuService.findAll();
            return new Result(true,MessageConst.ACTION_SUCCESS,menuList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("queryBean:{}",queryPageBean);
        try{
            // 调用Service获取分页结果数据
            PageResult pageResult = menuService.pageQuery(queryPageBean);
            // 成功，直接返回有内容的结果
            return pageResult;
        }catch(Exception e){
            e.printStackTrace();
        }
        // 失败，返回初始化结果，记录为0，数据为空
        return new PageResult(0L,null);
    }
    @RequestMapping("/add")
    public Result add(@RequestBody  Menu menu){
        log.debug("add:{}",menu);
        try{
            // 调用Service完成新增业务
            if(menuService != null) {
                menuService.add(menu);
                // 成功，返回添加成功消息
                return new Result(true, MessageConst.ACTION_SUCCESS);
            }else{
                log.debug("menuService is null ,未找到服务对象");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        // 失败，返回添加失败消息
        return new Result(false,MessageConst.ACTION_FAIL);
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            // 调用Service,基于ID获取检查项对象
            Menu menu = menuService.findById(id);
            // 成功，返回检查项对象数据
            return new Result(true,MessageConst.ACTION_SUCCESS,menu);
        }catch(Exception e){
            e.printStackTrace();
            // 失败，返回操作失败消息
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody  Menu menu){
        try{
            log.debug("edit checkItem:{}",menu);
            // 调用Service，更新数据
            menuService.edit(menu);
            // 成功，返回编辑成功消息
            return new Result(true,MessageConst.ACTION_SUCCESS);
        }catch(Exception e){
            e.printStackTrace();
            // 失败，返回编辑失败消息
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            // 调用Service删除检查项
            menuService.deleteById(id);
            // 成功，返回预定义成功消息
            return new Result(true,MessageConst.ACTION_SUCCESS);
        }catch(Exception e){
            e.printStackTrace();
            // 失败，返回异常消息
            return new Result(false,e.getMessage());
        }
    }

    /**
     * 根据用户名获得动态菜单
     * @param name
     * @return
     */
    @RequestMapping("/getMenu")
    public Result getMenuByUserName(String name){
        try{
            log.debug(">>>><<<<<   UserName:{}",name);
            //创建一个集合，封装菜单对应属性
            List<Map<String,Object>> mapList = menuService.getMenuBy(name);
            return new Result(true,MessageConst.ACTION_SUCCESS,mapList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }


>>>>>>> 6693e98994950fffdaffe5b5f8491e5db91a2da2
}
